//
//  ViewController.m
//  WindowTest
//
//  Created by wangrui on 13-10-15.
//  Copyright (c) 2013年 PPS. All rights reserved.
//

#import "ViewController.h"
#import "RootViewControllerTest.h"

@interface ViewController ()
{
    UIWindow *window;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    /*
     *接口兼容测试
     *
     *
         NSString *testStr = @"size";
         
         //被废弃的接口在高版本上是暂时可用的,不过未来某个时刻会不再支持
         CGSize rect = [testStr sizeWithFont:[UIFont systemFontOfSize:17]];
         
         //以下接口ios7以下的设备运行会崩溃,所以使用了 NS_AVAILABLE_IOS()的东西都要万分小心;
         CGSize rect2 = [testStr sizeWithAttributes: @{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
         NSLog(@"%@-%@",NSStringFromCGSize(rect),NSStringFromCGSize(rect2));
     *
     */
    
    
    /**
     * UIApplication keywindow 是否为nil测试
     *
        //现在仍旧输出的是nil
        //NSLog(@"view didload application:%p",[UIApplication sharedApplication].keyWindow);
     */
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)btclicked:(id)sender{
    NSLog(@"before show self.window is keywindow:%d",[self.view.window isKeyWindow]);

    /**
     * 测试代码 1: IOS7下不知道是不是storyboard的原因,无法显示window,需要在试一试
     * 经过测试又可以了，真机模拟器都可以正常运行,妈的,难道昨天是人品问题? --- 2013-10-16 11:50
     * 经调试在storybord的模式下,window不是实例变量则不可显示,两个条件都要到达. 有可能时苹果的bug.
     */
    window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];//window is Strong pointer
    window.windowLevel = UIWindowLevelAlert-10;
    window.backgroundColor = [UIColor blackColor];
    //window.alpha = 0.5;

    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.backgroundColor = [UIColor blueColor];
    bt.frame = CGRectMake(0, 0, 50, 50);
    [bt addTarget:self action:@selector(hideWindow:) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:bt];

    /**
     * 测试代码 2:
     * 键盘弹出时时显示在keywindow上的
     * UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 80 , 100, 50)];
     * tf.alpha = 1.0;
     * tf.backgroundColor = [UIColor whiteColor];
     * [window addSubview:tf];
    */
    [window makeKeyAndVisible];

    
    //现在有输出了,应该是在applicationDidFinishLoading函数结束后才设置 [UIApplication sharedApplication].keyWindow属性
    //NSLog(@"application:%p",[UIApplication sharedApplication].keyWindow);
    //NSLog(@"self.view.window:%p",self.view.window);
    
    
    
    /**
     * 测试代码 3: (这行代码在IOS7里不起作用)没设置plist时
     * 经测试在plist里加入 "View controller-based status bar appearance" 属性, 并设置为NO
     * 该代码在ios7上有效
     * 若设为Yes,IOS7上则会调用 prefersStatusBarHidden 来决定. 可以调-setNeedsStatusBarAppearanceUpdate
     * 来更新
     */
    [[UIApplication sharedApplication]setStatusBarHidden:YES];
    
    
    /**
     * 测试代码段 4: 调-setNeedsStatusBarAppearanceUpdate 来更新
     *
        if ([[UIDevice currentDevice].systemVersion floatValue]>=7.0) {
            [[UIApplication sharedApplication] setStatusBarHidden:YES];
            [self setNeedsStatusBarAppearanceUpdate];
        }
        else{
            [[UIApplication sharedApplication] setStatusBarHidden:YES];
        }
    */
    
    
    /*
     * 测试代码段 5: alertView 在自定义的window上显示测试
     *
     * 经测试alerView是显示在自己加的window之下,因为alert的级别是1999,而window设置的级别是2000
     * 为方便测试,把window的级别减10,alert显示在了window之上
     
     * aler show之后,alert所包含的window变成了keywindow,但是那个window并没有加入 [UIApplication sharedApplication].windows里
     *
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test"
                                                        message:nil
                                                        delegate:self
                                              cancelButtonTitle:@"ok"
                                              otherButtonTitles:nil, nil];
        [alert show];
     *
     */
    
    

    NSLog(@"after show self.window is keywindow:%d",[self.view.window isKeyWindow]);
    NSLog(@"after show addwindow is keywindow:%d",[window isKeyWindow]);
    NSLog(@"windows:%@",[UIApplication sharedApplication].windows);
    
    
    /**
     * 代码测试段 6: 模拟rootViewController的性质
     * 经测试: window 会自动旋转自己的rootViewController，且改变它的transform,跟之前测试结果一致
     * 释放时会减少对rootviewConroller的引用
     * 要释放一个自己创建的window,非arc,release到计数器为零即可. arc 上可以用strong pointer，然后设为nil
     */
    RootViewControllerTest *test = [[RootViewControllerTest alloc]init];
    window.rootViewController = test;
}


- (BOOL)prefersStatusBarHidden{
 
    return window.hidden;
}

- (void)hideWindow:(id)sender{
    
    //window 隐藏后原来的window自动变为keywindow
    window.hidden = YES;
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    NSLog(@"after hidden self.window is keywindow:%d",[self.view.window isKeyWindow]);
    NSLog(@"after hidden addwindow is keywindow:%d",[window isKeyWindow]);
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
{
    NSLog(@"after alert dismiss self.window is keywindow:%d",[self.view.window isKeyWindow]);
    NSLog(@"after alert dismiss addwindow is keywindow:%d",[window isKeyWindow]);
}

@end
